package myky.com.doctorapp.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView

import myky.com.doctorapp.R
import myky.com.doctorapp.entity.GroupItemEntity

/**
 * Adapter for our list of [GroupItemEntity]s.
 */
class DepartMentAdapter(private val mContext: Context, private val itemEntities: List<GroupItemEntity>) : BaseExpandableListAdapter() {
    private val inflater: LayoutInflater = LayoutInflater.from(mContext)

    override fun getChild(groupPosition: Int, childPosition: Int): GroupItemEntity.ChildItemeEntity {
        return itemEntities[groupPosition].items[childPosition]
    }

    override fun getChildId(groupPosition: Int, childPosition: Int): Long {
        return childPosition.toLong()
    }


    override fun getGroup(groupPosition: Int): GroupItemEntity {
        return itemEntities[groupPosition]
    }

    override fun getGroupCount(): Int {
        return itemEntities.size
    }

    override fun getChildrenCount(groupPosition: Int): Int {
        return itemEntities[groupPosition].items.size
    }

    override fun getGroupId(groupPosition: Int): Long {
        return groupPosition.toLong()
    }

    override fun getGroupView(groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup): View {
        var convertView = convertView
        val holder: GroupHolder
        val item = getGroup(groupPosition)
        if (convertView == null) {
            holder = GroupHolder()
            convertView = inflater.inflate(R.layout.group_item, parent, false)
            holder.title = convertView!!.findViewById(R.id.textTitle)
            holder.group_expand_state = convertView.findViewById(R.id.group_expand_state)
            holder.groupLayout = convertView.findViewById(R.id.pop_child_rl)
            convertView.tag = holder
        } else {
            holder = convertView.tag as GroupHolder
        }
        if (isExpanded) {
            holder.group_expand_state!!.setBackgroundResource(R.mipmap.ss_pick_up_icon)
        } else {
            holder.group_expand_state!!.setBackgroundResource(R.mipmap.ss_pull_down_icon)
        }

        if (item.isHasSelected) {
            holder.groupLayout!!.setBackgroundResource(R.color.white)
        } else {
            holder.groupLayout!!.setBackgroundResource(R.color.gray_eeeeee)
        }

        holder.title!!.text = item.title

        return convertView
    }

    override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean, convertView: View?, parent: ViewGroup): View {
        var convertView = convertView
        val holder: ChildHolder
        val item = getChild(groupPosition, childPosition)
        if (convertView == null) {
            holder = ChildHolder()
            convertView = inflater.inflate(R.layout.list_item, parent, false)
            holder.title = convertView!!.findViewById(R.id.textTitle)
            convertView.tag = holder
        } else {
            holder = convertView.tag as ChildHolder
        }
        if (item.isHasSelected) {
            holder.title!!.setTextColor(mContext.resources.getColor(R.color.colorPrimary))
        } else {
            holder.title!!.setTextColor(mContext.resources.getColor(R.color.gray_222222))
        }
        holder.title!!.text = item.title

        return convertView
    }

    override fun hasStableIds(): Boolean {
        return true
    }

    override fun isChildSelectable(arg0: Int, arg1: Int): Boolean {
        return true
    }

    private class ChildHolder {
        internal var title: TextView? = null
    }

    private class GroupHolder {
        internal var title: TextView? = null
        internal var group_expand_state: ImageView? = null
        internal var groupLayout: RelativeLayout? = null
    }
}
